package com.oauth.global.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.auth.client.rpc.MemberRemoteService;
import com.oauth.auth.client.rpc.MemberRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.header.HeaderUI;
import com.oauth.global.client.rpc.GlobalRemoteService;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;
import com.oauth.global.client.ui.BirthdayNotification;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ExtensionEntryPoint implements EntryPoint {

	private final MemberRemoteServiceAsync memberService = GWT.create(MemberRemoteService.class);

	private final GlobalRemoteServiceAsync globalService = GWT.create(GlobalRemoteService.class);

	private static ExtensionEntryPointUiBinder uiBinder = GWT
			.create(ExtensionEntryPointUiBinder.class);

	interface ExtensionEntryPointUiBinder extends UiBinder<DockLayoutPanel, ExtensionEntryPoint> {
	}

	@UiField
	HeaderUI header;

	@UiField
	BirthdayNotification birthdayNotification;

	public void onModuleLoad() {
		DockLayoutPanel p = uiBinder.createAndBindUi(this);

		memberService.getMember(new OAuthAsnyCallBack<MemberClientBean>() {
			public void onSuccess(MemberClientBean result) {
				if (result == null) {
					Window.Location.assign(GWT.getHostPageBaseURL());
				} else if (result.getName() != null && !result.getName().isEmpty()) {
					header.setUserName(result.getName());
				} else {
					header.setUserName(result.getEmail());
				}
			}
		});

		/*
		 * statusUpdate.setService(globalService); statusUpdate.initImages();
		 */
		birthdayNotification.setService(globalService);
		birthdayNotification.initBirthdays();
		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(p);
		// RootPanel.get().add(dockPanel);
	}

}
